package com.module.base.utils;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.ScrollView;


import androidx.recyclerview.widget.RecyclerView;


import static androidx.recyclerview.widget.RecyclerView.SCROLL_STATE_DRAGGING;
import static androidx.recyclerview.widget.RecyclerView.SCROLL_STATE_IDLE;
import static androidx.recyclerview.widget.RecyclerView.SCROLL_STATE_SETTLING;

/**
 * @author bsnl_yanxuwen
 * @date 2020/9/7 17:40
 * Description :
 */
public class ScrollUtils {
    private Context context;
    private View actionButton;
    private boolean disableFloatButton = false;
    private int distance;
    private boolean visible = true;
    private RecyclerView recyclerViewCur;//当前的，避免多个问题

    Handler handler = new Handler(Looper.getMainLooper());

    public ScrollUtils(Context context, View view, RecyclerView recyclerView) {
        this.context = context;
        this.actionButton = view;
        setRecyclerView(recyclerView);
    }

    public void setRecyclerView(RecyclerView recyclerView) {
        if (recyclerViewCur != null) {
            recyclerViewCur.removeOnScrollListener(scrollListener);
        }
        this.recyclerViewCur = recyclerView;
        if (actionButton != null && recyclerView != null) {
            recyclerView.addOnScrollListener(scrollListener);
        }
    }


    private RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener() {
        int state;

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (disableFloatButton) {
                recyclerView.removeOnScrollListener(this);
                return;
            }

            this.state = newState;
            switch (newState) {
                case SCROLL_STATE_IDLE: //空闲状态，停止滚动
                    handler.removeCallbacksAndMessages(null);
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                if (state == SCROLL_STATE_IDLE) {
                                    showFABAnimation(actionButton);
                                }
                            } catch (Exception e) {
                            }
                        }
                    }, 1000);
                    break;
                case SCROLL_STATE_DRAGGING: //手指拖动，滚动状态
                case SCROLL_STATE_SETTLING: //手指离开，惯性滚动状态
                    hideFABAnimation(actionButton);
                    handler.removeCallbacksAndMessages(null);
                    break;
            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            //顶部按钮使用
//            scrolled(dx, dy, recyclerView);
        }
    };

    private void scrolled(int dx, int dy, View view) {
        if (disableFloatButton) {
            if (view != null && view instanceof RecyclerView) {
                try {
                    ((RecyclerView) view).removeOnScrollListener(scrollListener);
                } catch (Exception e) {
                }
            } else if (view != null && view instanceof ScrollView) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    ((ScrollView) view).setOnScrollChangeListener(null);
                }
            }
            return;
        }
        if (distance < -ViewConfiguration.getTouchSlop() && !visible) {
            //显示fab
            //iv_go_uploading.setVisibility(View.VISIBLE);
            showFABAnimation(actionButton);
            distance = 0;
            visible = true;
        } else if (distance > ViewConfiguration.getTouchSlop() && visible) {
            //隐藏
            //iv_go_uploading.setVisibility(View.GONE);
            hideFABAnimation(actionButton);
            distance = 0;
            visible = false;
        }
        if ((dy > 0 && visible) || (dy < 0 && !visible))//向下滑并且可见  或者  向上滑并且不可见
            distance += dy;
    }

    /**
     * func:隐藏fab的动画
     */

    public void hideFABAnimation(View view) {
        if (view == null) {
            return;
        }
        PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 0f);
        PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 0f);
        PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 0f);

        ObjectAnimator end = ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY, pvhZ).setDuration(400);
        end.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                if (view != null) {
                    view.setVisibility(View.INVISIBLE);
                }
            }
        });
        end.start();
    }

    /**
     * func:显示fab动画
     */
    public void showFABAnimation(View view) {
        if (view == null) {
            return;
        }
        PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f);
        PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 1f);
        PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 1f);
        ObjectAnimator start = ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY, pvhZ).setDuration(400);
        start.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                if (view != null) {
                    view.setVisibility(View.VISIBLE);
                }
            }
        });
        start.start();
    }
}
